﻿using LaserAutoTest.Common.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LaserAutoTest.Common
{
    public static class DateTimeExtension
    {
        public static TimeSpan GetTimeDuration(this DateTime startTime, DateTime endTime)
        {
            TimeSpan start = new TimeSpan(startTime.Ticks);

            TimeSpan end = new TimeSpan(endTime.Ticks);

            TimeSpan ts = end.Subtract(start).Duration();
            return ts;
        }
        public static double GetTimeDurationMilliSeconds(this DateTime startTime, DateTime endTime)
        {
            TimeSpan ts = startTime.GetTimeDuration(endTime);
            return ts.TotalMilliseconds;
        }
        public static double GetTimeDurationSeconds(this DateTime startTime, DateTime endTime)
        {
            TimeSpan ts = startTime.GetTimeDuration(endTime);
            return ts.TotalSeconds;
        }
        public static double GetTimeDurationMinutes(this DateTime startTime, DateTime endTime)
        {
            TimeSpan ts = startTime.GetTimeDuration(endTime);
            return ts.TotalMinutes;
        }

        public static double GetTimeDurationHours(this DateTime startTime, DateTime endTime)
        {
            TimeSpan ts = startTime.GetTimeDuration(endTime);
            return ts.TotalHours;
        }

        public static double GetTimeDuration(this DateTime startTime, DateTime endTime, TimeUnitEnum timeUnit)
        {
            double duration = 0;
            switch (timeUnit)
            {
                case TimeUnitEnum.MilliSeconds:
                    duration = GetTimeDurationMilliSeconds(startTime, endTime);
                    break;
                case TimeUnitEnum.Seconds:
                    duration = GetTimeDurationSeconds(startTime, endTime);
                    break;
                case TimeUnitEnum.Minutes:
                    duration = GetTimeDurationMinutes(startTime, endTime);
                    break;
                case TimeUnitEnum.Hours:
                    duration = GetTimeDurationHours(startTime, endTime);
                    break;
            }
            return duration;
        }
    }
}
